<?php

namespace fast;

class CcProxy{
    private $username;
    private $password;
    private $address;
    private $header;
    private $http;
    
    public function __construct($address, $username, $password)
    {
        $this->username = $username;
        $this->password = $password;
        $this->address = $address;
        $this->http = new Http;
        $this->header = [
            "Authorization: Basic ".base64_encode($this->username.":".$this->password),
            "User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36"
        ];
    }

    public function index()
    {
        $response = $this->http->get($this->address . '/account', [], [CURLOPT_HTTPHEADER => $this->header]);

        if (empty($response)) {
            return [];
        }

        $search = '~<form name="form" method="post" action="account">([\w\W]*?)</form>~';
        preg_match_all($search, $response, $rr);
        if (count($rr) < 2) {
            return [];
        }

        $data = [];

        foreach ($rr[1] as $from) {
            preg_match_all('~name="userid" value="(.*?)"~', $from, $user_id);
            preg_match_all('~name="username" size="12" value="(.*?)"~', $from, $username);
            preg_match_all('~name="password" size="8" value="(.*?)"~', $from, $password);
            preg_match_all('~type="checkbox" name="enable" value="1" checked~', $from, $enable);
            preg_match_all('~type="checkbox" name="enable" value="1" checked~', $from, $enable);
            preg_match_all('~type="checkbox" name="usepassword" value="1" checked~', $from, $usePassword);
            preg_match_all('~type="checkbox" name="autodisable" value="1" checked~', $from, $autoDisable);
            preg_match_all('~name="disabledate" size="10" value="(.*?)"~', $from, $disableDate);
            preg_match_all('~name="disabletime" size="8" value="(.*?)"~', $from, $disableTime);

            if (count($user_id[1]) > 0) {
                $user = array(
                    "userid" => $user_id[1][0],
                    "username" => $username[1][0],
                    "enable" => count($enable[0]) > 0 ? 1 : 0,
                    "usepassword" => count($usePassword[0]) > 0 ? 1 : 0,
                    "password" => $password[1][0],
                    "autodisable" => count($autoDisable[0]) > 0 ? 1 : 0,
                    "disabledate" => $disableDate[1][0],
                    "disabletime" => $disableTime[1][0],
                );

                $expire_time = $user['disabledate']. ' ' .$user['disabletime'];

                if (strtotime($expire_time) < time()) {
                    $user['is_expire'] = 1;
                }else{
                    $user['is_expire'] = 0;
                }

                $data[] = $user;
            }
        }
        return $data;
    }

    public function add($username, $password, $disableDate, $disableTime, $ipAddress = "", $macAddress = "", $connection = "-1", $bandwidth = "-1")
    {
        $data = sprintf(
            'add=增加&username=%s&enable=1&password=%s&ipaddress=%s&macaddress=%s&connection=%s&bandwidth=%s&disabledate=%s&disabletime=%s&autodisable=1&usepassword=1',
            $username,$password, $ipAddress, $macAddress, $connection, $bandwidth, $disableDate,$disableTime
        );
        return $this->http->post($this->address . '/account', $data, [CURLOPT_HTTPHEADER => $this->header]);
    }


    public function edit($userId, $username, $password, $enable, $autoDisable, $usePassword, $disableDate, $disableTime, $ipAddress = "255.255.255.255", $macAddress = "", $connection = "-1", $bandwidth = "-1/-1") {
        $data = sprintf(
            'edit=编辑&userid=%s&username=%s&password=%s&ipaddress=%s&macaddress=%s&connection=%s&bandwidth=%s&disabledate=%s&disabletime=%s%s%s%s',
            $userId,$username, $password, $ipAddress, $macAddress, $connection, $bandwidth, $disableDate, $disableTime, $enable == 1 ? "&enable=$enable" : ""  , $autoDisable == 1 ? "&autodisable=$autoDisable" : "", $usePassword == 1 ? "&usepassword=$usePassword" : ""
        );
        return $this->http->post($this->address . '/account', $data, [CURLOPT_HTTPHEADER => $this->header]);
    }

    public function remove($userId) {
        $data = sprintf(
            'delete=删除&userid=%s',
            $userId
        );
        return $this->http->post($this->address . '/account', $data, [CURLOPT_HTTPHEADER => $this->header]);
    }

}
